Add support in libxenstore for using the xenbus_dev store connection.
authorcl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Sat, 3 Sep 2005 16:54:38 +0000 (16:54 +0000)
committercl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk>
Sat, 3 Sep 2005 16:54:38 +0000 (16:54 +0000)
Also add simple read/write/rm clients for command line access to the
store (using the xenbus_dev store connection).
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
.hgignore
tools/xenstore/Makefile
tools/xenstore/xenstore_client.c [new file with mode: 0644]
tools/xenstore/xs.c
tools/xenstore/xs.h
tools/xenstore/xs_lib.c
tools/xenstore/xs_lib.h

index 0bc47f730a8e2deba5013bdc6d70df45af135bd3..c99199863536da16d6d8134df3e66f432856eaf6 100644 (file)
--- a/.hgignore
+++ b/.hgignore
 ^tools/xenstat/xentop/xentop$
 ^tools/xenstore/testsuite/tmp/.*$
 ^tools/xenstore/xen$
+^tools/xenstore/xenbus_dev.h$
 ^tools/xenstore/xenstored$
 ^tools/xenstore/xenstored_test$
+^tools/xenstore/xenstore-read$
+^tools/xenstore/xenstore-rm$
+^tools/xenstore/xenstore-write$
 ^tools/xenstore/xs_dom0_test$
 ^tools/xenstore/xs_random$
 ^tools/xenstore/xs_stress$
index 992f93bc8214e28499320b74a031b6f747ede8d5..aa35033f6dde265d2f0b5254e7048b2ae99bf626 100644 (file)
@@ -24,16 +24,28 @@ TESTDIR  = `pwd`/testsuite/tmp
 TESTFLAGS= -DTESTING
 TESTENV  = XENSTORED_ROOTDIR=$(TESTDIR) XENSTORED_RUNDIR=$(TESTDIR)
 
-all: xen xenstored libxenstore.so
+CLIENTS := xenstore-read xenstore-rm xenstore-write
+CLIENTS_OBJS := $(patsubst xenstore-%,xenstore_%.o,$(CLIENTS))
+
+all: xen xenbus_dev.h libxenstore.so xenstored $(CLIENTS)
 
 testcode: xen xs_test xenstored_test xs_random xs_dom0_test
 
 xen:
        ln -sf $(XEN_ROOT)/xen/include/public $@
 
+xenbus_dev.h:
+       ln -sf $(XEN_ROOT)/linux-2.6-xen-sparse/include/asm-xen/linux-public/xenbus_dev.h $@
+
 xenstored: xenstored_core.o xenstored_watch.o xenstored_domain.o xenstored_transaction.o xs_lib.o talloc.o utils.o
        $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -lxenctrl -o $@
 
+$(CLIENTS): xenstore-%: xenstore_%.o
+       $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -lxenctrl -L. -lxenstore -o $@
+
+$(CLIENTS_OBJS): xenstore_%.o: xenstore_client.c
+       $(COMPILE.c) -DCLIENT_$(*F) -o $@ $<
+
 xenstored_test: xenstored_core_test.o xenstored_watch_test.o xenstored_domain_test.o xenstored_transaction_test.o xs_lib.o talloc_test.o fake_libxc.o utils.o
        $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
 
@@ -111,12 +123,13 @@ TAGS:
 tarball: clean
        cd .. && tar -c -j -v -h -f xenstore.tar.bz2 xenstore/
 
-install: xenstored libxenstore.so
+install: libxenstore.so xenstored $(CLIENTS)
        $(INSTALL_DIR) -p $(DESTDIR)/var/run/xenstored
        $(INSTALL_DIR) -p $(DESTDIR)/var/lib/xenstored
        $(INSTALL_DIR) -p $(DESTDIR)/usr/sbin
        $(INSTALL_DIR) -p $(DESTDIR)/usr/include
        $(INSTALL_PROG) xenstored $(DESTDIR)/usr/sbin
+       $(INSTALL_PROG) $(CLIENTS) $(DESTDIR)/usr/bin
        $(INSTALL_DIR) -p $(DESTDIR)/usr/$(LIBDIR)
        $(INSTALL_DATA) libxenstore.so $(DESTDIR)/usr/$(LIBDIR)
        $(INSTALL_DATA) xs.h $(DESTDIR)/usr/include
diff --git a/tools/xenstore/xenstore_client.c b/tools/xenstore/xenstore_client.c
new file mode 100644 (file)
index 0000000..842aca3
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License.  See the file "COPYING" in the main directory of
+ * this archive for more details.
+ *
+ * Copyright (C) 2005 by Christian Limpach
+ *
+ */
+
+#include <err.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <xs.h>
+
+static void
+usage(const char *progname)
+{
+#if defined(CLIENT_read)
+    errx(1, "Usage: %s [-h] [-p] key [...]", progname);
+#elif defined(CLIENT_write)
+    errx(1, "Usage: %s [-h] key value [...]", progname);
+#elif defined(CLIENT_rm)
+    errx(1, "Usage: %s [-h] key [...]", progname);
+#endif
+}
+
+int
+main(int argc, char **argv)
+{
+    struct xs_handle *xsh;
+    bool success;
+    int ret = 0;
+#if defined(CLIENT_read)
+    char *val;
+    int prefix = 0;
+#endif
+
+    xsh = xs_domain_open();
+    if (xsh == NULL)
+       err(1, "xs_domain_open");
+
+    while (1) {
+       int c, index = 0;
+       static struct option long_options[] = {
+           {"help", 0, 0, 'h'},
+#if defined(CLIENT_read)
+           {"prefix", 0, 0, 'p'},
+#endif
+           {0, 0, 0, 0}
+       };
+
+       c = getopt_long(argc, argv, "h"
+#if defined(CLIENT_read)
+                       "p"
+#endif
+                       , long_options, &index);
+       if (c == -1)
+           break;
+
+       switch (c) {
+       case 'h':
+           usage(argv[0]);
+           /* NOTREACHED */
+#if defined(CLIENT_read)
+       case 'p':
+           prefix = 1;
+           break;
+#endif
+       }
+    }
+
+    if (optind == argc) {
+       usage(argv[0]);
+       /* NOTREACHED */
+    }
+#if defined(CLIENT_write)
+    if ((argc - optind) % 1) {
+       usage(argv[0]);
+       /* NOTREACHED */
+    }
+#endif
+
+    /* XXX maybe find longest common prefix */
+    success = xs_transaction_start(xsh, "/");
+    if (!success)
+       errx(1, "couldn't start transaction");
+
+    while (optind < argc) {
+#if defined(CLIENT_read)
+       val = xs_read(xsh, argv[optind], NULL);
+       if (val == NULL) {
+           warnx("couldn't read path %s", argv[optind]);
+           ret = 1;
+           goto out;
+       }
+       if (prefix)
+           printf("%s: ", argv[optind]);
+       printf("%s\n", val);
+       free(val);
+       optind++;
+#elif defined(CLIENT_write)
+       success = xs_write(xsh, argv[optind], argv[optind + 1],
+                          strlen(argv[optind + 1]), O_CREAT);
+       if (!success) {
+           warnx("could not write path %s", argv[optind]);
+           ret = 1;
+           goto out;
+       }
+       optind += 2;
+#elif defined(CLIENT_rm)
+       success = xs_rm(xsh, argv[optind]);
+       if (!success) {
+           warnx("could not remove path %s", argv[optind]);
+           ret = 1;
+           goto out;
+       }
+       optind++;
+#endif
+    }
+
+ out:
+    success = xs_transaction_end(xsh, ret ? true : false);
+    if (!success)
+       errx(1, "couldn't end transaction");
+
+    return ret;
+}
index de1e0dd736648636c71b0e4795443f72374e9cde..c49fded8ecca690b8f832a67463ab2faabe655af 100644 (file)
 #include <signal.h>
 #include <stdint.h>
 #include <errno.h>
+#include <sys/ioctl.h>
 #include "xs.h"
 #include "xenstored.h"
 #include "xs_lib.h"
 #include "utils.h"
+#include "xenbus_dev.h"
 
 struct xs_handle
 {
        int fd;
+       enum { SOCK, DEV } type;
 };
 
 /* Get the socket from the store daemon handle.
@@ -65,13 +68,35 @@ static struct xs_handle *get_socket(const char *connect_to)
                h = malloc(sizeof(*h));
                if (h) {
                        h->fd = sock;
+                       h->type = SOCK;
                        return h;
                }
        }
 
        saved_errno = errno;
        close(sock);
-       free(h);
+       errno = saved_errno;
+       return NULL;
+}
+
+static struct xs_handle *get_dev(const char *connect_to)
+{
+       int fd, saved_errno;
+       struct xs_handle *h = NULL;
+
+       fd = open(connect_to, O_RDONLY);
+       if (fd < 0)
+               return NULL;
+
+       h = malloc(sizeof(*h));
+       if (h) {
+               h->fd = fd;
+               h->type = DEV;
+               return h;
+       }
+
+       saved_errno = errno;
+       close(fd);
        errno = saved_errno;
        return NULL;
 }
@@ -86,6 +111,11 @@ struct xs_handle *xs_daemon_open_readonly(void)
        return get_socket(xs_daemon_socket_ro());
 }
 
+struct xs_handle *xs_domain_open(void)
+{
+       return get_dev(xs_domain_dev());
+}
+
 void xs_daemon_close(struct xs_handle *h)
 {
        if (h->fd >= 0)
@@ -160,9 +190,9 @@ static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
 }
 
 /* Send message to xs, get malloc'ed reply.  NULL and set errno on error. */
-static void *xs_talkv(struct xs_handle *h, enum xsd_sockmsg_type type,
-                     const struct iovec *iovec, unsigned int num_vecs,
-                     unsigned int *len)
+static void *xs_talkv_sock(struct xs_handle *h, enum xsd_sockmsg_type type,
+                          const struct iovec *iovec, unsigned int num_vecs,
+                          unsigned int *len)
 {
        struct xsd_sockmsg msg;
        void *ret = NULL;
@@ -223,6 +253,54 @@ close_fd:
        return NULL;
 }
 
+/* Send message to xs, get malloc'ed reply.  NULL and set errno on error. */
+static void *xs_talkv_dev(struct xs_handle *h, enum xsd_sockmsg_type type,
+                         const struct iovec *iovec, unsigned int num_vecs,
+                         unsigned int *len)
+{
+       struct xenbus_dev_talkv dt;
+       char *buf;
+       int err, buflen = 1024;
+
+ again:
+       buf = malloc(buflen);
+       if (buf == NULL) {
+               errno = ENOMEM;
+               return NULL;
+       }
+       dt.type = type;
+       dt.iovec = (struct kvec *)iovec;
+       dt.num_vecs = num_vecs;
+       dt.buf = buf;
+       dt.len = buflen;
+       err = ioctl(h->fd, IOCTL_XENBUS_DEV_TALKV, &dt);
+       if (err < 0) {
+               free(buf);
+               errno = err;
+               return NULL;
+       }
+       if (err > buflen) {
+               free(buf);
+               buflen = err;
+               goto again;
+       }
+       if (len)
+               *len = err;
+       return buf;
+}
+
+/* Send message to xs, get malloc'ed reply.  NULL and set errno on error. */
+static void *xs_talkv(struct xs_handle *h, enum xsd_sockmsg_type type,
+                     const struct iovec *iovec, unsigned int num_vecs,
+                     unsigned int *len)
+{
+       if (h->type == SOCK)
+               return xs_talkv_sock(h, type, iovec, num_vecs, len);
+       if (h->type == DEV)
+               return xs_talkv_dev(h, type, iovec, num_vecs, len);
+       return NULL;
+}
+
 /* free(), but don't change errno. */
 static void free_no_errno(void *p)
 {
index 98e98a9d1481400b45358be8b7b299ee15acd707..a16c8209996ce22c372dd4a758a799b0ae0f2cbe 100644 (file)
@@ -30,6 +30,7 @@ struct xs_handle;
  * Returns a handle or NULL.
  */
 struct xs_handle *xs_daemon_open(void);
+struct xs_handle *xs_domain_open(void);
 
 /* Connect to the xs daemon (readonly for non-root clients).
  * Returns a handle or NULL.
index 629eca59487fe7f68d1672b94f8cb4a8c804e8e3..4c355b990230f3c79d2f651891e3503a9da0a7bb 100644 (file)
@@ -66,6 +66,12 @@ const char *xs_daemon_transactions(void)
        return buf;
 }
 
+const char *xs_domain_dev(void)
+{
+       char *s = getenv("XENSTORED_DOMAIN_DEV");
+       return (s ? s : "/proc/xen/xenbus");
+}
+
 /* Simple routines for writing to sockets, etc. */
 bool xs_write_all(int fd, const void *data, unsigned int len)
 {
index 4e99c2aa9c201805bc07fd49a003f2789191fe4b..f0289584c5a5904d49da66bc14377c5daf528dc6 100644 (file)
@@ -48,6 +48,7 @@ const char *xs_daemon_socket(void);
 const char *xs_daemon_socket_ro(void);
 const char *xs_daemon_store(void);
 const char *xs_daemon_transactions(void);
+const char *xs_domain_dev(void);
 
 /* Simple write function: loops for you. */
 bool xs_write_all(int fd, const void *data, unsigned int len);